<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="用户年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="用户年龄"></el-input>
    </el-form-item>
    <el-form-item label="用户电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="用户电话"></el-input>
    </el-form-item>
    <el-form-item label="用户分类0：系统管理员，1：出租用户，2：求租用户" prop="category">
      <el-input v-model="dataForm.category" placeholder="用户分类0：系统管理员，1：出租用户，2：求租用户"></el-input>
    </el-form-item>
    <el-form-item label="出租出去的房屋" prop="rentouts">
      <el-input v-model="dataForm.rentouts" placeholder="出租出去的房屋"></el-input>
    </el-form-item>
    <el-form-item label="求租的房屋" prop="rents">
      <el-input v-model="dataForm.rents" placeholder="求租的房屋"></el-input>
    </el-form-item>
    <el-form-item label="启用状态[0 - 禁用，1 - 启用]" prop="status">
      <el-input v-model="dataForm.status" placeholder="启用状态[0 - 禁用，1 - 启用]"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          password: '',
          age: '',
          phone: '',
          category: '',
          rentouts: '',
          rents: '',
          status: ''
        },
        dataRule: {
          name: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '用户年龄不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '用户电话不能为空', trigger: 'blur' }
          ],
          category: [
            { required: true, message: '用户分类0：系统管理员，1：出租用户，2：求租用户不能为空', trigger: 'blur' }
          ],
          rentouts: [
            { required: true, message: '出租出去的房屋不能为空', trigger: 'blur' }
          ],
          rents: [
            { required: true, message: '求租的房屋不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '启用状态[0 - 禁用，1 - 启用]不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/rentHouse/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.user.name
                this.dataForm.password = data.user.password
                this.dataForm.age = data.user.age
                this.dataForm.phone = data.user.phone
                this.dataForm.category = data.user.category
                this.dataForm.rentouts = data.user.rentouts
                this.dataForm.rents = data.user.rents
                this.dataForm.status = data.user.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rentHouse/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'password': this.dataForm.password,
                'age': this.dataForm.age,
                'phone': this.dataForm.phone,
                'category': this.dataForm.category,
                'rentouts': this.dataForm.rentouts,
                'rents': this.dataForm.rents,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
